//
//  CCProgress.m
//  didiAnimation
//
//  Created by iOS app dev on 17/2/21.
//  Copyright © 2017年 007qilv. All rights reserved.
//

#import "CCProgress.h"

@implementation CCProgress


- (instancetype) init
{
    if ((self = [super init])) {
        [self setupLayer];
    }
    return self;
}


- (void)layoutSublayers
{
    self.path = [self bezierPathWithArcCenter];
    self.progress.path = self.path;
    
    self.thumb.frame = CGRectMake((320 - kProgressThumbWh) / 2.0f, 180, kProgressThumbWh, kProgressThumbWh);
    [super layoutSublayers];
}



- (void)setupLayer
{
    // 绘制圆
    self.path = [self bezierPathWithArcCenter];
    self.fillColor = [UIColor clearColor].CGColor;
    self.strokeColor = [UIColor colorWithRed:0.86f green:0.86f blue:0.86f alpha:0.4f].CGColor;
    self.lineWidth = 2;
    
    // 添加可以变动的滚动条
    self.progress = [CAShapeLayer layer];
    self.progress.path = self.path;
    self.progress.fillColor = [UIColor clearColor].CGColor;
    self.progress.strokeColor = [UIColor whiteColor].CGColor;
    self.progress.lineWidth = 4;
    self.progress.lineCap = kCALineCapSquare;
    self.progress.lineJoin = kCALineCapSquare;
    [self addSublayer:self.progress];
    
    // 添加可以旋转的ThumbLayer
    self.thumb = [[CCProgressThumb alloc] init];
    [self addSublayer:self.thumb];
}


/**
 *  得到bezier曲线路劲
 *  @return
 */
- (CGPathRef)bezierPathWithArcCenter
{
    CGFloat centerX = CGRectGetWidth(self.frame) / 2.0;
    CGFloat centerY = CGRectGetHeight(self.frame) / 2.0;
    return [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY)
                                          radius:centerX
                                      startAngle:(- M_PI / 2)
                                        endAngle:(3 * M_PI / 2)
                                       clockwise:YES].CGPath;
}





- (void)setCircleColor:(UIColor *)circleColor
{
    self.progress.strokeColor = circleColor.CGColor;
}


- (void)setAnimtionTime:(NSTimeInterval)animtionTime
{
    _animationTime = animtionTime;
    [self.thumb setAnimationTime:animtionTime];
}


- (void)setElapsedTime:(NSTimeInterval)elapsedTime
{
    _initialProgress = [self calculatePercent:_elapsedTime toTime:_animationTime];
    _elapsedTime = elapsedTime;
    
    self.progress.strokeEnd = self.percent;
    [self startAnimation];
}

- (double)percent
{
    _percent = [self calculatePercent:_elapsedTime toTime:_animationTime];
    return _percent;
}
- (double)calculatePercent:(NSTimeInterval)fromTime toTime:(NSTimeInterval)toTime
{
    double progress = 0.0f;
    if ((toTime > 0) && (fromTime > 0)) {
        progress = fromTime / toTime;
        if ((progress * 100) > 100) {
            progress = 1.0f;
        }
    }
    return progress;
}



- (void)startAnimation
{
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = kAnimTimeInterval;
    pathAnimation.fromValue = @(self.initialProgress);
    pathAnimation.toValue = @(self.percent);
    pathAnimation.removedOnCompletion = YES;
    [self.progress addAnimation:pathAnimation forKey:nil];
    
    [self.thumb startAnimation];
    self.thumb.timeLabel.text = [self stringFromTimeInterval:_elapsedTime shorTime:YES];
}




- (void)stopAnimation
{
    _elapsedTime = 0;
    self.progress.strokeEnd = 0.0;
    [self removeAllAnimations];
    [self.thumb stopAnimation];
}




/**
 *  时间格式转换
 *  @param interval NSTimeInterval
 *  @param shortTime BOOL
 *  @return
 */
- (NSString *)stringFromTimeInterval:(NSTimeInterval)interval shorTime:(BOOL)shortTime
{
    NSInteger ti = (NSInteger)interval;
    NSInteger seconds = ti % 60;
    NSInteger minutes = (ti / 60) % 60;
    NSInteger hours = (ti / 3600);
    if (shortTime) {
        return [NSString stringWithFormat:@"%02ld:%02ld", (long)hours, (long)seconds];
    } else {
        return [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)hours, (long)minutes, (long)seconds];
    }
}
@end
